package dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseConnectionPool {
	private static DataSource ds;

	public DatabaseConnectionPool()
	{
	}
	
	static{
		try {
			Context initialContext = new InitialContext();
			Context dbContext = (Context)initialContext.lookup("java:comp/env");
			ds = (DataSource)dbContext.lookup("jdbc/QSS");
			
		} catch (NamingException e) {
			e.printStackTrace();
			//System.out.println("Naming exception inside DatabaseConnection constructor");
		}
	}
	
	public static Connection getConnection()
	{
		Connection conn=null;
			try {
				conn = ds.getConnection();
			} catch (SQLException e) {
				//e.printStackTrace();
				System.out.println("Failed to create SQL connection");
			}
		return conn;
	}
	
	public boolean setFeedback(String userID, String responses[])
	{
		boolean result = false;
		try {
			int num = 100+(int)(Math.random()*999);
			String FeedbackID = "Feedback"+num;
			String query = "Insert into Feedback(`FeedbackID`, `UserID`, `OverallScore`, `GamePlayScore`, `MarketPlaceScore`, `AccountInfoScore`, `DisplayScore`) Values('"+FeedbackID+"',"+"'user34',"+responses[0]+","+responses[1]+","+responses[2]
				+","+responses[3]+","+responses[4]+")";
			Connection conn = getConnection();
			Statement stmt = conn.createStatement();
			result = stmt.execute(query);
		
		if(stmt.getUpdateCount()>0)
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
